﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Collections;
using LookupFieldEntityPicker.Helpers;


namespace LookupFieldEntityPicker.CustomLookup
{
    public class RecordPicker : EntityEditorWithPicker
    {
        public bool HidePicker { get; set; }

        public string FieldSeachColumns { get; set; }


        //public RecordPicker()
        //{
        //        PickerDialogType = typeof(RecordPickerDialog);
        //        ValidatorEnabled = true;
        //        AllowTypeIn = true;
        //        MultiSelect = false;
        //    CustomProperty = "test";
        //}

        protected override void OnInit(EventArgs e)
        {
            PickerDialogType = typeof(RecordPickerDialog);
            ValidatorEnabled = true;
            AllowTypeIn = true;
            MultiSelect = false;
            CustomProperty = FieldSeachColumns;

        }
        public override void Validate()
        {
            if (ErrorPlaceHolder != null)
            {
                ErrorPlaceHolder.Controls.Clear();
                ErrorPlaceHolder.Controls.Add(ErrorLabel);
            }
            base.Validate();
        }


        public override PickerEntity ValidateEntity(PickerEntity needsValidation)
        {
            PickerEntity result;
           
            if (needsValidation.IsResolved)
            {
                result = needsValidation;
                
            }
            else
            {
                string displayText = needsValidation.DisplayText;

                List<DepartmentEntity> list = ListHelpers.FindDepartments(Constants.DEPARTMENT_CODE, displayText);
                
                switch (list.Count)
                {
                    case 0:
                        {
                            needsValidation.Description = "No match";
                            break;
                        }
                    case 1:
                        {
                            DepartmentEntity department = list[0];
                            needsValidation.Key = department.ID.ToString();
                            needsValidation.DisplayText = department.Code;
                            needsValidation.Description = department.Code + ":" + department.Name;
                            needsValidation.IsResolved = true;
                            needsValidation.EntityData.Add(department.ID.ToString(), department.Code);
                            break;
                        }
                    default:
                        {
                            needsValidation.Description = "Multiple matches";
                            break;
                        }
                }
                result = needsValidation;
            }
          
            return result;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!HidePicker) return;

            var type = typeof(EntityEditorWithPicker);
            var fieldInfo = type.GetField("Browse", BindingFlags.NonPublic | BindingFlags.Instance);

            if (fieldInfo == null) return;
            var browse = (HyperLink)fieldInfo.GetValue(this);
            browse.Visible = false;
        }

       
    }
}
